<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>算术运算符</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/operator_arithmetic","wgTitle":"cpp/language/operator arithmetic","wgCurRevisionId":62662,"wgArticleId":2735,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Pages with unreviewed LWG DR marker"],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/operator_arithmetic","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for text
 * CSS class: source-text, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text  {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_operator_arithmetic skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">算术运算符</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="https://zh.cppreference.com/w/cpp/container" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="expressions.html" title="cpp/language/expressions"> 表达式</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> 概述 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="value_category.html" title="cpp/language/value category"> 值类别</a>（左值 lvalue、右值 rvalue、亡值 xvalue）</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="eval_order.html" title="cpp/language/eval order"> 求值顺序</a>（序列点）</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="constant_expression.html" title="cpp/language/constant expression"> 常量表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="expressions.html#.E4.B8.8D.E6.B1.82.E5.80.BC.E8.A1.A8.E8.BE.BE.E5.BC.8F" title="cpp/language/expressions"> 不求值表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="expressions.html#.E5.88.9D.E7.AD.89.E8.A1.A8.E8.BE.BE.E5.BC.8F" title="cpp/language/expressions"> 初等表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="lambda.html" title="cpp/language/lambda">lambda 表达式</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 字面量 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="integer_literal.html" title="cpp/language/integer literal">整数字面量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="floating_literal.html" title="cpp/language/floating literal">浮点字面量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="bool_literal.html" title="cpp/language/bool literal">布尔字面量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="character_literal.html" title="cpp/language/character literal">字符字面量</a>，包含<a href="escape.html" title="cpp/language/escape">转义序列</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="string_literal.html" title="cpp/language/string literal">字符串字面量</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="nullptr.html" title="cpp/language/nullptr">空指针字面量</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="user_literal.html" title="cpp/language/user literal">用户定义字面量</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 运算符 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_assignment.html" title="cpp/language/operator assignment"> 赋值运算符</a>:
<code>a=b</code>, <code>a+=b</code>, <code>a-=b</code>, <code>a*=b</code>, <code>a/=b</code>, <code>a%=b</code>, <code>a&amp;=b</code>, <code>a|=b</code>, <code>a^=b</code>, <code>a&lt;&lt;=b</code>, <code>a&gt;&gt;=b</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_incdec.html" title="cpp/language/operator incdec"> 自增与自减</a>: <code>++a</code>, <code>--a</code>, <code>a++</code>, <code>a--</code></td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> 算术运算符</strong>:
<code>+a</code>, <code>-a</code>, <code>a+b</code>, <code>a-b</code>, <code>a*b</code>, <code>a/b</code>, <code>a%b</code>, <code>~a</code>, <code>a&amp;b</code>, <code>a|b</code>, <code>a^b</code>, <code>a&lt;&lt;b</code>, <code>a&gt;&gt;b</code> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_logical.html" title="cpp/language/operator logical"> 逻辑运算符</a>: <code>a||b</code>, <code>a&amp;&amp;b</code>, <code>!a</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_comparison.html" title="cpp/language/operator comparison"> 比较运算符</a>: <code>a==b</code>, <code>a!=b</code>, <code>a&lt;b</code>, <code>a&gt;b</code>, <code>a&lt;=b</code>, <code>a&gt;=b</code>, <code>a&lt;=&gt;b</code><span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_member_access.html" title="cpp/language/operator member access"> 成员访问运算符</a>: <code>a[b]</code>, <code>*a</code>, <code>&amp;a</code>, <code>a-&gt;b</code>, <code>a.b</code>, <code>a-&gt;*b</code>, <code>a.*b</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_other.html" title="cpp/language/operator other"> 其他运算符</a>: <code>a(...)</code>, <code>a,b</code>, <code>a?b:c</code> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="default_comparisons.html" title="cpp/language/default comparisons">默认比较</a><span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_alternative.html" title="cpp/language/operator alternative">运算符的代用表示</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_precedence.html" title="cpp/language/operator precedence">优先级和结合性</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="fold.html" title="cpp/language/fold">折叠表达式</a><span class="t-mark-rev t-since-cxx17">(C++17)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="new.html" title="cpp/language/new">new 表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="delete.html" title="cpp/language/delete">delete 表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="throw.html" title="cpp/language/throw">throw 表达式</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="alignof.html" title="cpp/language/alignof">alignof</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="sizeof.html" title="cpp/language/sizeof">sizeof</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="sizeof....html" title="cpp/language/sizeof...">sizeof...</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="typeid.html" title="cpp/language/typeid">typeid</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="noexcept.html" title="cpp/language/noexcept">noexcept</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operators.html" title="cpp/language/operators">运算符重载</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> 类型转换 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="implicit_cast.html" title="cpp/language/implicit conversion">隐式转换</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="const_cast.html" title="cpp/language/const cast">const_cast</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static_cast.html" title="cpp/language/static cast">static_cast</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="reinterpret_cast.html" title="cpp/language/reinterpret cast">reinterpret_cast</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="dynamic_cast.html" title="cpp/language/dynamic cast">dynamic_cast</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="explicit_cast.html" title="cpp/language/explicit cast">显式转换</a> <code>(T)a</code>, <code>T(a)</code></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="cast_operator.html" title="cpp/language/cast operator">用户定义转换</a></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>返回特定算术运算的结果。
</p>
<table class="wikitable" style="font-size:85%;">

<tr>
<th rowspan="2"> 运算符名
</th>
<th rowspan="2"> 语法
</th>
<th rowspan="2"> <a href="operators.html" title="cpp/language/operators">可重载</a>
</th>
<th colspan="2"> 原型示例（对于 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">class</span> T</span></span>）
</th></tr>
<tr>
<th> 类内定义
</th>
<th> 类外定义
</th></tr>
<tr>
<td> 一元加
</td>
<td> <code>+a</code>
</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">是
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T T<span class="sy4">::</span><span class="me2">operator</span><span class="sy2">+</span><span class="br0">(</span><span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></span>
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T operator<span class="sy2">+</span><span class="br0">(</span><span class="kw4">const</span> T <span class="sy3">&amp;</span>a<span class="br0">)</span><span class="sy4">;</span></span></span>
</td></tr>
<tr>
<td> 一元减
</td>
<td> <code>-a</code>
</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">是
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T T<span class="sy4">::</span><span class="me2">operator</span><span class="sy2">-</span><span class="br0">(</span><span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></span>
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T operator<span class="sy2">-</span><span class="br0">(</span><span class="kw4">const</span> T <span class="sy3">&amp;</span>a<span class="br0">)</span><span class="sy4">;</span></span></span>
</td></tr>
<tr>
<td> 加法
</td>
<td> <code>a + b</code>
</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">是
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T T<span class="sy4">::</span><span class="me2">operator</span><span class="sy2">+</span><span class="br0">(</span><span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></span>
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T operator<span class="sy2">+</span><span class="br0">(</span><span class="kw4">const</span> T <span class="sy3">&amp;</span>a, <span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span><span class="sy4">;</span></span></span>
</td></tr>
<tr>
<td> 减法
</td>
<td> <code>a - b</code>
</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">是
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T T<span class="sy4">::</span><span class="me2">operator</span><span class="sy2">-</span><span class="br0">(</span><span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></span>
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T operator<span class="sy2">-</span><span class="br0">(</span><span class="kw4">const</span> T <span class="sy3">&amp;</span>a, <span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span><span class="sy4">;</span></span></span>
</td></tr>
<tr>
<td> 乘法
</td>
<td> <code>a * b</code>
</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">是
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T T<span class="sy4">::</span><span class="me2">operator</span><span class="sy2">*</span><span class="br0">(</span><span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></span>
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T operator<span class="sy2">*</span><span class="br0">(</span><span class="kw4">const</span> T <span class="sy3">&amp;</span>a, <span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span><span class="sy4">;</span></span></span>
</td></tr>
<tr>
<td> 除法
</td>
<td> <code>a / b</code>
</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">是
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T T<span class="sy4">::</span><span class="me2">operator</span><span class="sy2">/</span><span class="br0">(</span><span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></span>
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T operator<span class="sy2">/</span><span class="br0">(</span><span class="kw4">const</span> T <span class="sy3">&amp;</span>a, <span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span><span class="sy4">;</span></span></span>
</td></tr>
<tr>
<td> 模
</td>
<td> <code>a % b</code>
</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">是
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T T<span class="sy4">::</span><span class="me2">operator</span><span class="sy2">%</span><span class="br0">(</span><span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></span>
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T operator<span class="sy2">%</span><span class="br0">(</span><span class="kw4">const</span> T <span class="sy3">&amp;</span>a, <span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span><span class="sy4">;</span></span></span>
</td></tr>
<tr>
<td> 逐位非
</td>
<td> <code>~a</code>
</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">是
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T T<span class="sy4">::</span><span class="me2">operator</span>~<span class="br0">(</span><span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></span>
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T operator~<span class="br0">(</span><span class="kw4">const</span> T <span class="sy3">&amp;</span>a<span class="br0">)</span><span class="sy4">;</span></span></span>
</td></tr>
<tr>
<td> 逐位与
</td>
<td> <code>a &amp; b</code>
</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">是
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T T<span class="sy4">::</span><span class="me2">operator</span><span class="sy3">&amp;</span><span class="br0">(</span><span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></span>
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T operator<span class="sy3">&amp;</span><span class="br0">(</span><span class="kw4">const</span> T <span class="sy3">&amp;</span>a, <span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span><span class="sy4">;</span></span></span>
</td></tr>
<tr>
<td> 逐位或
</td>
<td> <code>a | b</code>
</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">是
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T T<span class="sy4">::</span><span class="me2">operator</span><span class="sy3">|</span><span class="br0">(</span><span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></span>
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T operator<span class="sy3">|</span><span class="br0">(</span><span class="kw4">const</span> T <span class="sy3">&amp;</span>a, <span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span><span class="sy4">;</span></span></span>
</td></tr>
<tr>
<td> 逐位异或
</td>
<td> <code>a ^ b</code>
</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">是
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T T<span class="sy4">::</span><span class="me2">operator</span><span class="sy3">^</span><span class="br0">(</span><span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></span>
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T operator<span class="sy3">^</span><span class="br0">(</span><span class="kw4">const</span> T <span class="sy3">&amp;</span>a, <span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span><span class="sy4">;</span></span></span>
</td></tr>
<tr>
<td> 逐位左移
</td>
<td> <code>a &lt;&lt; b</code>
</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">是
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T T<span class="sy4">::</span><span class="me2">operator</span><span class="sy1">&lt;&lt;</span><span class="br0">(</span><span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></span>
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T operator<span class="sy1">&lt;&lt;</span><span class="br0">(</span><span class="kw4">const</span> T <span class="sy3">&amp;</span>a, <span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span><span class="sy4">;</span></span></span>
</td></tr>
<tr>
<td> 逐位右移
</td>
<td> <code>a &gt;&gt; b</code>
</td>
<td style="background: #90ff90; color: black; vertical-align: middle; text-align: center;" class="table-yes">是
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T T<span class="sy4">::</span><span class="me2">operator</span><span class="sy1">&gt;&gt;</span><span class="br0">(</span><span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span> <span class="kw4">const</span><span class="sy4">;</span></span></span>
</td>
<td> <span class="t-c"><span class="mw-geshi cpp source-cpp">T operator<span class="sy1">&gt;&gt;</span><span class="br0">(</span><span class="kw4">const</span> T <span class="sy3">&amp;</span>a, <span class="kw4">const</span> T2 <span class="sy3">&amp;</span>b<span class="br0">)</span><span class="sy4">;</span></span></span>
</td></tr>
<tr>
<td colspan="5">
<dl><dd><b>注解</b><br>
</dd></dl>
<ul><li> 所有内建运算符都返回值，而大多数<a href="operators.html" title="cpp/language/operators">用户定义重载</a>亦返回值，从而能以与内建版本相同的方式使用用户定义运算符。不过，用户定义重载中，任何类型都可以作为返回类型（包括 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">void</span></span></span>）。特别是，<code>operator&lt;&lt;</code> 和 <code>operator&gt;&gt;</code> 为流插入和流抽取的重载所返回的是 <code>T&amp;</code>。
</li><li> <code>T2</code> 可以为包括 <code>T</code> 在内的任何类型。
</li></ul>
</td></tr></table>
<h3><span class="mw-headline" id=".E8.A7.A3.E9.87.8A">解释</span></h3>
<p>所有算术运算符计算特定算术运算的结果，并返回其结果。不修改实参。
</p>
<h4><span class="mw-headline" id=".E8.BD.AC.E6.8D.A2">转换</span></h4>
<p>若传递给算术运算符的操作数是整型或无作用域枚举类型，则在任何其他行动前（但在左值到右值转换后，若其适用），对操作数实施<a href="implicit_cast.html#.E6.95.B4.E5.9E.8B.E6.8F.90.E5.8D.87" title="cpp/language/implicit conversion">整型提升</a>。若操作数之一具有数组或函数类型，则实施<a href="implicit_cast.html#.E6.95.B0.E7.BB.84.E5.88.B0.E6.8C.87.E9.92.88.E8.BD.AC.E6.8D.A2" title="cpp/language/implicit conversion">数组到指针</a>和<a href="implicit_cast.html#.E5.87.BD.E6.95.B0.E5.88.B0.E6.8C.87.E9.92.88" title="cpp/language/implicit conversion">函数到指针</a>转换。
</p><p>对于（除移位之外的）二元运算符，当提升后的操作数拥有不同类型时，实施称为<i>一般算术转换 (usual arithmetic conversion)</i>的一组额外的隐式转换，目的是产生<i>公共类型 (common type)</i>（亦可由 <span class="t-lc"><a href="../types/common_type.html" title="cpp/types/common type">std::common_type</a></span> 类型特性访问）。<span class="t-rev-inl t-since-cxx20"><span>若在任何整型提升前，一个操作数为枚举类型而另一操作数为浮点类型或不同的枚举类型，则此行为被弃用。</span> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></span></span>
</p>
<ul><li> 若任一操作数具有<a href="enum.html#.E6.9C.89.E4.BD.9C.E7.94.A8.E5.9F.9F.E6.9E.9A.E4.B8.BE" title="cpp/language/enum">有作用域枚举类型</a>，则不进行转换：另一操作数和返回类型必须具有相同类型
</li></ul>
<ul><li> 否则，若任一操作数为 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">long</span> <span class="kw4">double</span></span></span>，则将另一操作数转换为 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">long</span> <span class="kw4">double</span></span></span>
</li></ul>
<ul><li> 否则，若任一操作数为 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">double</span></span></span>，则将另一操作数转换为 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">double</span></span></span>
</li></ul>
<ul><li> 否则，若任一操作数为 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">float</span></span></span>，则将另一操作数转换为 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">float</span></span></span>
</li></ul>
<ul><li> 否则，操作数拥有整数类型（因为 <code>bool</code>、<code>char</code>、<code>char8_t</code>、<code>char16_t</code>、<code>char32_t</code>、<code>wchar_t</code> 和无作用域枚举在此时已被提升），并对其实施<a href="implicit_cast.html#.E6.95.B4.E5.9E.8B.E8.BD.AC.E6.8D.A2" title="cpp/language/implicit conversion">整型转换</a>以产生如下的公共类型：
</li></ul>
<dl><dd><ul><li> 若两个操作数均为有符号或均为无符号，则将拥有较小<i>转换等级</i>的操作数转换为拥有较大整数转换等级的操作数的类型。
</li></ul>
</dd></dl>
<dl><dd><ul><li> 否则，若无符号操作数的转换等级大于或等于有符号操作数的转换等级，则将有符号操作数转换为无符号操作数的类型。
</li></ul>
</dd></dl>
<dl><dd><ul><li> 否则，若有符号操作数的类型能表示无符号操作数类型的所有值，则将无符号操作数转换为有符号操作数的类型。
</li></ul>
</dd></dl>
<dl><dd><ul><li> 否则，将两个操作数都转换为有符号操作数的无符号对应版本。
</li></ul>
</dd></dl>
<p><i>转换等级（conversion rank）</i>按 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span></span></span>、<span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">signed</span> <span class="kw4">char</span></span></span>、<span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">short</span></span></span>、<span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">int</span></span></span>、<span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">long</span></span></span>、<span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">long</span> <span class="kw4">long</span></span></span> 顺序递增。任何无符号类型的等级都等于其对应的有符号类型的等级。<span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">char</span></span></span> 的等级等于 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">signed</span> <span class="kw4">char</span></span></span> 和 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">unsigned</span> <span class="kw4">char</span></span></span> 的等级。<span class="t-c"><span class="mw-geshi cpp source-cpp">char8_t</span></span>、<span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">char16_t</span></span></span>、<span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">char32_t</span></span></span> 和 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">wchar_t</span></span></span> 的等级等于其底层类型的等级。
</p>
<h4><span class="mw-headline" id=".E6.BA.A2.E5.87.BA">溢出</span></h4>
<p>无符号整数算术始终进行 <span class="texhtml" style="white-space: nowrap;">modulo 2<span class="t-su">n<br></span></span>，其中 n 是该整数的位数。例如对于 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">unsigned</span> <span class="kw4">int</span></span></span>，向 <span class="t-lc"><a href="../types/climits.html" title="cpp/types/climits">UINT_MAX</a></span> 加一得到 <span class="t-c"><span class="mw-geshi cpp source-cpp">​<span class="nu0">0</span>​</span></span>，而从 <span class="t-c"><span class="mw-geshi cpp source-cpp">​<span class="nu0">0</span>​</span></span> 减一得到 <span class="t-lc"><a href="../types/climits.html" title="cpp/types/climits">UINT_MAX</a></span>。
</p><p>有符号整数算术运算溢出（结果类型无法容纳其结果）时，行为未定义：它可能按照表示法的规则（典型为补码）发生回绕，可能在某些平台上或由于编译器选项（例如 GCC 和 Clang 中的 <code>-ftrapv</code>）引发陷阱，也可能完全<a rel="nofollow" class="external text" href="http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html">被编译器优化掉</a>。
</p>
<h4><span class="mw-headline" id=".E6.B5.AE.E7.82.B9.E7.8E.AF.E5.A2.83">浮点环境</span></h4>
<p>若支持 <a href="../preprocessor/impl.html" title="cpp/preprocessor/impl"><tt>
#pragma STDC FENV_ACCESS</tt></a> 并设置它为 <code>ON</code>，则所有浮点算术运算符都服从当前浮点<a href="../numeric/fenv/FE_round.html" title="cpp/numeric/fenv/FE round">舍入方向</a>，并报告 <a href="../numeric/math/math_errhandling.html" title="cpp/numeric/math/math errhandling"><tt>math_errhandling</tt></a> 中指定的浮点算术错误，除非是发生于<a href="initialization.html#.E9.9D.9E.E5.B1.80.E9.83.A8.E5.8F.98.E9.87.8F" title="cpp/language/initialization">静态初始化器</a>之中（该情况下不引发浮点异常，而舍入模式为最近舍入）。
</p>
<h4><span class="mw-headline" id=".E6.B5.AE.E7.82.B9.E7.BC.A9.E7.95.A5">浮点缩略</span></h4>
<p>除非支持 <a href="../preprocessor/impl.html" title="cpp/preprocessor/impl"><tt>
#pragma STDC FP_CONTRACT</tt></a> 并设置它为 <code>OFF</code>，否则所有浮点算术都可以如同其中间结果拥有无限范围和精度一般进行，即允许实施省略舍入误差和浮点异常的优化。例如 C++ 允许以单条融合乘加 CPU 指令来实现 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="br0">(</span>x<span class="sy2">*</span>y<span class="br0">)</span> <span class="sy2">+</span> z</span></span>，或把 <span class="t-c"><span class="mw-geshi cpp source-cpp">a <span class="sy1">=</span> x<span class="sy2">*</span>x<span class="sy2">*</span>x<span class="sy2">*</span>x<span class="sy4">;</span></span></span> 优化为 <span class="t-c"><span class="mw-geshi cpp source-cpp">tmp <span class="sy1">=</span> x <span class="sy2">*</span>x<span class="sy4">;</span> a <span class="sy1">=</span> tmp<span class="sy2">*</span>tmp</span></span>。
</p><p>无关乎缩略，浮点算术的中间结果可拥有与其类型所指定不同的范围和精度，见 <span class="t-lc"><a href="../types/climits/FLT_EVAL_METHOD.html" title="cpp/types/climits/FLT EVAL METHOD">FLT_EVAL_METHOD</a></span>。
</p><p>正式而言，C++ 标准不在浮点运算的精度上做任何保证。
</p>
<h4><span class="mw-headline" id=".E4.B8.80.E5.85.83.E7.AE.97.E6.9C.AF.E8.BF.90.E7.AE.97.E7.AC.A6">一元算术运算符</span></h4>
<p>一元算术运算符表达式的形式为
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>+</b></code> <span class="t-spar">表达式</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>-</b></code> <span class="t-spar">表达式</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 一元加（提升）。</div>
<div class="t-li1"><span class="t-li"></span> 对于内建运算符，<span class="t-spar">表达式</span> 必须拥有算术、无作用域枚举或指针类型。若操作数具有整型或无作用域枚举类型，则对其实施整型提升，并确定结果类型。</div>
<div class="t-li1"><span class="t-li">2)</span> 一元减（取反）。</div>
<div class="t-li1"><span class="t-li"></span> 对于内建运算符，<span class="t-spar">表达式</span> 必须拥有算术或无作用域枚举类型。对操作数实施整型提升，并确定结果类型。</div>
<p>内建的一元加运算符返回其操作数的值。仅有的非无操作情形，是当操作数拥有整型类型或无作用域枚举类型时，它将被整型提升所更改，例如将 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">char</span></span></span> 转换为 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">int</span></span></span>，或者当操作数服从左值到右值、数组到指针或函数到指针转换时。
</p><p>内建的一元减运算符计算其提升后操作数的相反数。对于无符号的 <code>a</code>，<code>-a</code> 的值为 <span class="texhtml" style="white-space: nowrap;">2<span class="t-su">b<br></span>-a</span>，其中 <code>b</code> 为提升后的位数。
</p><p>在<a href="overload_resolution.html#.E8.B0.83.E7.94.A8.E9.87.8D.E8.BD.BD.E8.BF.90.E7.AE.97.E7.AC.A6" title="cpp/language/overload resolution">针对用户定义运算符的重载决议</a>中，对于每个提升后算术类型 <code>A</code> 和每个类型 <code>T</code>，下列函数签名参与重载决议：
</p>
<table class="t-dcl-begin"><tbody>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">A operator<span class="sy2">+</span><span class="br0">(</span>A<span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">T<span class="sy2">*</span> operator<span class="sy2">+</span><span class="br0">(</span>T<span class="sy2">*</span><span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">A operator<span class="sy2">-</span><span class="br0">(</span>A<span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr> 
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
</tbody></table>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">char</span> c <span class="sy1">=</span> <span class="nu12">0x6a</span><span class="sy4">;</span>
    <span class="kw4">int</span> n1 <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
    <span class="kw4">unsigned</span> <span class="kw4">char</span> n2 <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
    <span class="kw4">unsigned</span> <span class="kw4">int</span> n3 <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"char: "</span> <span class="sy1">&lt;&lt;</span> c <span class="sy1">&lt;&lt;</span> <span class="st0">" int: "</span> <span class="sy1">&lt;&lt;</span> <span class="sy2">+</span>c <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span> <span class="st0">"-1, where 1 is signed: "</span> <span class="sy1">&lt;&lt;</span> <span class="sy2">-</span>n1 <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span> <span class="st0">"-1, where 1 is unsigned char: "</span> <span class="sy1">&lt;&lt;</span> <span class="sy2">-</span>n2 <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span> <span class="st0">"-1, where 1 is unsigned int: "</span> <span class="sy1">&lt;&lt;</span> <span class="sy2">-</span>n3 <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
    <span class="kw4">char</span> a<span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="sy4">;</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"size of array: "</span> <span class="sy1">&lt;&lt;</span> sizeof a <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span> <span class="st0">"size of pointer: "</span> <span class="sy1">&lt;&lt;</span> sizeof <span class="sy2">+</span>a <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">char: j int: 106
-1, where 1 is signed: -1
-1, where 1 is unsigned char: -1
-1, where 1 is unsigned int: 4294967295
size of array: 3
size of pointer: 8</pre></div></div> 
</div>
<h4><span class="mw-headline" id=".E5.8A.A0.E6.80.A7.E8.BF.90.E7.AE.97.E7.AC.A6">加性运算符</span></h4>
<p>二元加性算术运算符表达式的形式为
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>+</b></code> <span class="t-spar">rhs</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>-</b></code> <span class="t-spar">rhs</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 加法</div>
<div class="t-li1"><span class="t-li"></span> 对于内建运算符，<span class="t-spar">lhs</span> 和 <span class="t-spar">rhs</span> 必须为下列之一：
<dl><dd><ul><li> 都拥有算术或无作用域枚举类型。此情况下，对两个操作数都实施一般算术转换，并确定结果类型。
</li><li> 一个是指向完整定义的对象的指针类型，另一个拥有整型或无作用域枚举类型。此情况下，结果具有指针的类型。</li></ul></dd></dl></div>


<div class="t-li1"><span class="t-li">2)</span> 减法</div>
<div class="t-li1"><span class="t-li"></span> 对于内建运算符，<span class="t-spar">lhs</span> 和 <span class="t-spar">rhs</span> 必须为下列之一：
<dl><dd><ul><li> 都拥有算术或无作用域枚举类型。此情况下，对两个操作数都实施一般算术转换，并确定结果类型。
</li><li> <span class="t-spar">lhs</span> 为指向完整定义的对象类型的指针，<span class="t-spar">rhs</span> 拥有整型或无作用域枚举类型。此情况下，结果具有指针的类型。
</li><li> 忽略 cv 限定符时，两者都是指向相同完整定义的对象类型的指针。此情况下，结果类型为 <span class="t-lc"><a href="../types/ptrdiff_t.html" title="cpp/types/ptrdiff t">std::ptrdiff_t</a></span>。</li></ul></dd></dl></div>


<p>对于算术或枚举类型的操作数，二元加的结果是操作数（经一般算术转换后）的和，而二元减运算符的结果是第一操作数减去第二操作数（经一般算术转换后）的结果，但是，如果该类型支持 IEEE 浮点算术（见 <span class="t-lc"><a href="../types/numeric_limits/is_iec559.html" title="cpp/types/numeric limits/is iec559">std::numeric_limits::is_iec559</a></span>），
</p>
<dl><dd><ul><li> 若操作数之一为 NaN，则结果为 NaN
</li><li> 无穷减无穷为 NaN，并引发 <span class="t-lc"><a href="../numeric/fenv/FE_exceptions.html" title="cpp/numeric/fenv/FE exceptions">FE_INVALID</a></span>
</li><li> 无穷加负无穷为 NaN，并引发 <span class="t-lc"><a href="../numeric/fenv/FE_exceptions.html" title="cpp/numeric/fenv/FE exceptions">FE_INVALID</a></span>
</li></ul>
</dd></dl>
<p>若任何操作数为指针，则适用下列规则：
</p>
<ul><li> 把指向非数组对象的指针当做指向大小为 1 的数组的首元素的指针。
</li><li> 若指针 <code>P</code> 指向数组的第 <code>i</code> 个元素，则表达式 <code>P+n</code>、<code>n+P</code> 和 <code>P-n</code> 分别是指向同一数组的第 <code>i+n</code>、第 <code>i+n</code> 和第 <code>i-n</code> 个元素的同类型指针。指针加法的结果亦可为末尾后一位置（one past the end）的指针（即使得 <code>P-1</code> 指向数组末元素的指针 <code>P</code>）。任何其他情形（即试图生成不指向同一数组的任何元素或同一数组末尾后一位置的指针）均引起未定义行为。
</li><li> 若指针 <code>P</code> 指向数组的第 <code>i</code> 个元素，而指针 <code>Q</code> 指向同一数组的第 <code>j</code> 个元素，则表达式 <code>P-Q</code> 拥有值 <span class="t-c"><span class="mw-geshi cpp source-cpp">i<span class="sy2">-</span>j</span></span>（若 <span class="t-lc"><a href="../types/ptrdiff_t.html" title="cpp/types/ptrdiff t">std::ptrdiff_t</a></span> 可容纳该值）。两个操作数都必须指向同一数组的元素（或末尾后一位置），否则行为未定义。若结果不能由 <span class="t-lc"><a href="../types/ptrdiff_t.html" title="cpp/types/ptrdiff t">std::ptrdiff_t</a></span> 容纳，则行为未定义。
</li><li> 任何情况下，如果所指向的类型与数组元素类型不同（其中对于每一层元素，若它们自身即为指针，则不考虑其 cv 限定性），则指针算术的行为未定义。特别是，以指向某个派生对象数组中的元素的指向基类指针做指针算术是未定义的。
</li><li> 对指针加或减 <span class="t-c"><span class="mw-geshi cpp source-cpp">​<span class="nu0">0</span>​</span></span>，结果就是该指针，没有更改。若两个指针指向同一对象，或都指向同一数组的末尾后一位置，或都是空指针，则减法的结果等于 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="br0">(</span><a href="../types/ptrdiff_t.html"><span class="kw101">std::<span class="me2">ptrdiff_t</span></span></a><span class="br0">)</span><span class="nu0">0</span></span></span>。
</li></ul>
<p>这些指针算术运算符，允许指针得以满足<a href="../named_req/RandomAccessIterator.html" title="cpp/named req/RandomAccessIterator"><i>遗留随机访问迭代器</i> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(LegacyRandomAccessIterator)</span> </a>的要求。
</p><p>在<a href="overload_resolution.html#.E8.B0.83.E7.94.A8.E9.87.8D.E8.BD.BD.E8.BF.90.E7.AE.97.E7.AC.A6" title="cpp/language/overload resolution">针对用户定义运算符的重载决议</a>中，对于每对提升后的算术类型 <code>L</code> 和 <code>R</code> 和每个对象类型 <code>T</code>，下列函数签名参与重载决议：
</p>
<table class="t-dcl-begin"><tbody>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">LR operator<span class="sy2">+</span><span class="br0">(</span>L, R<span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">LR operator<span class="sy2">-</span><span class="br0">(</span>L, R<span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">T<span class="sy2">*</span> operator<span class="sy2">+</span><span class="br0">(</span>T<span class="sy2">*</span>, <a href="../types/ptrdiff_t.html"><span class="kw101">std::<span class="me2">ptrdiff_t</span></span></a><span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">T<span class="sy2">*</span> operator<span class="sy2">+</span><span class="br0">(</span><a href="../types/ptrdiff_t.html"><span class="kw101">std::<span class="me2">ptrdiff_t</span></span></a>, T<span class="sy2">*</span><span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">T<span class="sy2">*</span> operator<span class="sy2">-</span><span class="br0">(</span>T<span class="sy2">*</span>, <a href="../types/ptrdiff_t.html"><span class="kw101">std::<span class="me2">ptrdiff_t</span></span></a><span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp"><a href="../types/ptrdiff_t.html"><span class="kw101">std::<span class="me2">ptrdiff_t</span></span></a> operator<span class="sy2">-</span><span class="br0">(</span>T<span class="sy2">*</span>, T<span class="sy2">*</span><span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
</tbody></table>
<p>其中 <code>LR</code> 是对 <code>L</code> 和 <code>R</code> 实施一般算术转换的结果。
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">char</span> c <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>
    <span class="kw4">unsigned</span> <span class="kw4">int</span> un <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>
    <span class="kw4">int</span>  n <span class="sy1">=</span> <span class="sy2">-</span><span class="nu0">10</span><span class="sy4">;</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span>  <span class="st0">" 2 + (-10), where 2 is a char    = "</span> <span class="sy1">&lt;&lt;</span> c <span class="sy2">+</span> n <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span>  <span class="st0">" 2 + (-10), where 2 is unsigned  = "</span> <span class="sy1">&lt;&lt;</span> un <span class="sy2">+</span> n <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span>  <span class="st0">" -10 - 2.12  = "</span> <span class="sy1">&lt;&lt;</span> n <span class="sy2">-</span> <span class="nu16">2.12</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
 
    <span class="kw4">char</span> a<span class="br0">[</span><span class="nu0">4</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="st0">'a'</span>, <span class="st0">'b'</span>, <span class="st0">'c'</span>, <span class="st0">'d'</span><span class="br0">}</span><span class="sy4">;</span>
    <span class="kw4">char</span><span class="sy2">*</span> p <span class="sy1">=</span> <span class="sy3">&amp;</span>a<span class="br0">[</span><span class="nu0">1</span><span class="br0">]</span><span class="sy4">;</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"Pointer addition examples: "</span> <span class="sy1">&lt;&lt;</span> <span class="sy2">*</span>p <span class="sy1">&lt;&lt;</span> <span class="sy2">*</span><span class="br0">(</span>p <span class="sy2">+</span> <span class="nu0">2</span><span class="br0">)</span>
              <span class="sy1">&lt;&lt;</span> <span class="sy2">*</span><span class="br0">(</span><span class="nu0">2</span> <span class="sy2">+</span> p<span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="sy2">*</span><span class="br0">(</span>p <span class="sy2">-</span> <span class="nu0">1</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
    <span class="kw4">char</span><span class="sy2">*</span> p2 <span class="sy1">=</span> <span class="sy3">&amp;</span>a<span class="br0">[</span><span class="nu0">4</span><span class="br0">]</span><span class="sy4">;</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"Pointer difference: "</span> <span class="sy1">&lt;&lt;</span> p2 <span class="sy2">-</span> p <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">2 + (-10), where 2 is a char    = -8
 2 + (-10), where 2 is unsigned  = 4294967288
 -10 - 2.12  = -12.12
Pointer addition examples: bdda
Pointer difference: 3</pre></div></div> 
</div>
<h4><span class="mw-headline" id=".E4.B9.98.E6.80.A7.E8.BF.90.E7.AE.97.E7.AC.A6">乘性运算符</span></h4>
<p>二元乘性运算符表达式的形式为
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>*</b></code> <span class="t-spar">rhs</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>/</b></code> <span class="t-spar">rhs</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>%</b></code> <span class="t-spar">rhs</span>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 乘法</div>
<div class="t-li1"><span class="t-li"></span> 对于内建运算符，<span class="t-spar">lhs</span> 和 <span class="t-spar">rhs</span> 必须都具有算术或无作用域枚举类型。</div>
<div class="t-li1"><span class="t-li">2)</span> 除法</div>
<div class="t-li1"><span class="t-li"></span> 对于内建运算符，<span class="t-spar">lhs</span> 和 <span class="t-spar">rhs</span> 必须都具有算术或无作用域枚举类型。</div>
<div class="t-li1"><span class="t-li">3)</span> 求余</div>
<div class="t-li1"><span class="t-li"></span> 对于内建运算符，<span class="t-spar">lhs</span> 和 <span class="t-spar">rhs</span> 必须都具有算术或无作用域枚举类型。</div>
<p>所有三个运算符，均对两个操作数实施一般算术转换，并确定结果类型。
</p><p>二元运算符 * 进行其操作数（经一般算术转换后）的乘法，但对于浮点乘法，
</p>
<ul><li> NaN 乘以任何数均得 NaN
</li><li> 无穷乘以零得 NaN 并引发 <span class="t-lc"><a href="../numeric/fenv/FE_exceptions.html" title="cpp/numeric/fenv/FE exceptions">FE_INVALID</a></span>
</li></ul>
<p>二元运算符 / 将第一操作数除以第二操作数（经一般算术转换后）。
</p><p>对于整型操作数，它产生代数商，
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx11"><td>
<p>以实现定义的方向对商进行舍入。
</p>
</td>
<td><span class="t-mark-rev t-until-cxx11">(C++11 前)</span></td></tr>
<tr class="t-rev t-since-cxx11"><td>
<p>商向零取整（舍弃小数部分）。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<p>若第二操作数为零，则行为未定义，但若进行浮点除法且类型支持 IEEE 浮点算术（见 <span class="t-lc"><a href="../types/numeric_limits/is_iec559.html" title="cpp/types/numeric limits/is iec559">std::numeric_limits::is_iec559</a></span>），则：
</p>
<ul><li> 若操作数之一为 NaN，则结果为 NaN
</li><li> 非零数除以 ±0.0 得到符号正确的无穷，并引发 <span class="t-lc"><a href="../numeric/fenv/FE_exceptions.html" title="cpp/numeric/fenv/FE exceptions">FE_DIVBYZERO</a></span>
</li><li> 0.0 除以 0.0 得 NaN，并引发 <span class="t-lc"><a href="../numeric/fenv/FE_exceptions.html" title="cpp/numeric/fenv/FE exceptions">FE_INVALID</a></span>
</li></ul>
<p>二元运算符 % 产生以第二操作数整除第一操作数的余数（经一般算术转换后；注意各操作数的类型必须都为整型类型）。若商 <code>a/b</code> 能以结果类型表示，则 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="br0">(</span>a<span class="sy2">/</span>b<span class="br0">)</span><span class="sy2">*</span>b <span class="sy2">+</span> a<span class="sy2">%</span>b <span class="sy1">==</span> a</span></span>。若第二操作数为零，则行为未定义。若商 <code>a/b</code> 不能以结果类型表示，则 <code>a/b</code> 和 <code>a%b</code> 的行为均未定义（这意味着，补码系统上 <span class="t-c"><span class="mw-geshi cpp source-cpp">INT_MIN<span class="sy2">%-</span><span class="nu0">1</span></span></span> 为未定义）。
</p><p>注意：C++11 之前，若二元运算符 % 的一或两个操作数为负，则余数符号是实现定义的，因为它取决于整数除法的舍入方向。函数 <span class="t-lc"><a href="../numeric/math/div.html" title="cpp/numeric/math/div">std::div</a></span> 在该情况下提供良好定义的行为。
</p><p>注意：对于浮点余数，见 <span class="t-lc"><a href="../numeric/math/remainder.html" title="cpp/numeric/math/remainder">std::remainder</a></span> 和 <span class="t-lc"><a href="../numeric/math/fmod.html" title="cpp/numeric/math/fmod">std::fmod</a></span>。
</p><p>在<a href="overload_resolution.html#.E8.B0.83.E7.94.A8.E9.87.8D.E8.BD.BD.E8.BF.90.E7.AE.97.E7.AC.A6" title="cpp/language/overload resolution">针对用户定义运算符的重载决议中</a>，对于每对提升后的算术类型 <code>LA</code> 与 <code>RA</code> 及每对提升后的整型类型 <code>LI</code> 与 <code>RI</code>，下列函数签名参与重载决议：
</p>
<table class="t-dcl-begin"><tbody>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">LRA operator<span class="sy2">*</span><span class="br0">(</span>LA, RA<span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">LRA operator<span class="sy2">/</span><span class="br0">(</span>LA, RA<span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">LRI operator<span class="sy2">%</span><span class="br0">(</span>LI, RI<span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
</tbody></table>
<p>其中 <code>LRx</code> 是对 <code>Lx</code> 和 <code>Rx</code> 实施一般算术转换的结果。
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">char</span> c <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>
    <span class="kw4">unsigned</span> <span class="kw4">int</span> un <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>
    <span class="kw4">int</span>  n <span class="sy1">=</span> <span class="sy2">-</span><span class="nu0">10</span><span class="sy4">;</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span>  <span class="st0">"2 * (-10), where 2 is a char    = "</span> <span class="sy1">&lt;&lt;</span> c <span class="sy2">*</span> n <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span>  <span class="st0">"2 * (-10), where 2 is unsigned  = "</span> <span class="sy1">&lt;&lt;</span> un <span class="sy2">*</span> n <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span>  <span class="st0">"-10 / 2.12  = "</span> <span class="sy1">&lt;&lt;</span> n <span class="sy2">/</span> <span class="nu16">2.12</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span>  <span class="st0">"-10 / 21  = "</span> <span class="sy1">&lt;&lt;</span> n <span class="sy2">/</span> <span class="nu0">21</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span>  <span class="st0">"-10 % 21  = "</span> <span class="sy1">&lt;&lt;</span> n <span class="sy2">%</span> <span class="nu0">21</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">2 * (-10), where 2 is a char    = -20
2 * (-10), where 2 is unsigned  = 4294967276
-10 / 2.12  = -4.71698
-10 / 21  = 0
-10 % 21  = -10</pre></div></div> 
</div>
<h4><span class="mw-headline" id=".E9.80.90.E4.BD.8D.E9.80.BB.E8.BE.91.E8.BF.90.E7.AE.97.E7.AC.A6">逐位逻辑运算符</span></h4>
<p>逐位算术运算符表达式的形式为
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>~</b></code> <span class="t-spar">rhs</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>&amp;</b></code> <span class="t-spar">rhs</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>|</b></code> <span class="t-spar">rhs</span>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>^</b></code> <span class="t-spar">rhs</span>
</td>
<td> (4)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 逐位非（NOT）</div>
<div class="t-li1"><span class="t-li">2)</span> 逐位与（AND）</div>
<div class="t-li1"><span class="t-li">3)</span> 逐位或（OR）</div>
<div class="t-li1"><span class="t-li">4)</span> 逐位异或（XOR）</div>
<div class="t-li1"><span class="t-li"></span> 对于内建运算符，<span class="t-spar">lhs</span> 和 <span class="t-spar">rhs</span> 必须都拥有整型或无作用域枚举类型。对两个操作数实施一般算术转换，并确定结果类型。</div>
<p>operator~ 的结果是实参值（提升后）的逐位非（反码，一的补码）。operator&amp; 的结果是两个操作数值（经一般算术转换后）的逐位与。operator| 的结果是两个操作数值（经一般算术转换后）的逐位或。operator^ 的结果是两个操作数值（经一般算术转换后）的逐位异或。
</p><p>在<a href="overload_resolution.html#.E8.B0.83.E7.94.A8.E9.87.8D.E8.BD.BD.E8.BF.90.E7.AE.97.E7.AC.A6" title="cpp/language/overload resolution">针对用户定义运算符的重载决议</a>中，对于每对提升后的整型类型 <code>L</code> 和 <code>R</code>，下列函数签名参与重载决议：
</p>
<table class="t-dcl-begin"><tbody>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">R operator~<span class="br0">(</span>R<span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">LR operator<span class="sy3">&amp;</span><span class="br0">(</span>L, R<span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">LR operator<span class="sy3">^</span><span class="br0">(</span>L, R<span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">LR operator<span class="sy3">|</span><span class="br0">(</span>L, R<span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
</tbody></table>
<p>其中 <code>LR</code> 是对 <code>L</code> 和 <code>R</code> 实施一般算术转换的结果。
</p>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <a href="../io/manip/hex.html"><span class="kw1782">std::<span class="me2">hex</span></span></a> <span class="sy1">&lt;&lt;</span> <a href="../io/manip/showbase.html"><span class="kw1766">std::<span class="me2">showbase</span></span></a><span class="sy4">;</span>
    uint16_t mask <span class="sy1">=</span> <span class="nu12">0x00f0</span><span class="sy4">;</span>
    uint32_t a <span class="sy1">=</span> <span class="nu12">0x12345678</span><span class="sy4">;</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"Value: "</span> <span class="sy1">&lt;&lt;</span> a <span class="sy1">&lt;&lt;</span> <span class="st0">" mask: "</span> <span class="sy1">&lt;&lt;</span> mask <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span> <span class="st0">"Setting bits: "</span> <span class="sy1">&lt;&lt;</span> <span class="br0">(</span>a <span class="sy3">|</span> mask<span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span> <span class="st0">"Clearing bits: "</span> <span class="sy1">&lt;&lt;</span> <span class="br0">(</span>a <span class="sy3">&amp;</span> ~mask<span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span> <span class="st0">"Selecting bits: "</span> <span class="sy1">&lt;&lt;</span> <span class="br0">(</span>a <span class="sy3">&amp;</span> mask<span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">Value: 0x12345678 mask: 0xf0
Setting bits: 0x123456f8
Clearing bits: 0x12345608
Selecting bits: 0x70</pre></div></div> 
</div>
<h4><span class="mw-headline" id=".E7.A7.BB.E4.BD.8D.E8.BF.90.E7.AE.97.E7.AC.A6">移位运算符</span></h4>
<p>移位运算符表达式的形式为
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>&lt;&lt;</b></code> <span class="t-spar">rhs</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">lhs</span> <code><b>&gt;&gt;</b></code> <span class="t-spar">rhs</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 将 <span class="t-spar">lhs</span> 左移 <span class="t-spar">rhs</span> 位</div>
<div class="t-li1"><span class="t-li">2)</span> 将 <span class="t-spar">lhs</span> 右移 <span class="t-spar">rhs</span> 位</div>
<div class="t-li1"><span class="t-li"></span> 对于内建运算符，<span class="t-spar">lhs</span> 和 <span class="t-spar">rhs</span> 必须都具有整型或无作用域枚举类型。对两个运算数实施整型提升。</div>
<p>返回类型是提升后的左操作数的类型。
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx20"><td>
<p>对于无符号 <code>a</code>，<code>a &lt;&lt; b</code> 的值是 <span class="texhtml" style="white-space: nowrap;">a * 2<span class="t-su">b<br></span></span> 对 <span class="texhtml" style="white-space: nowrap;">2<span class="t-su">N<br></span></span> 取模，其中 <span class="texhtml" style="white-space: nowrap;">N</span> 是返回类型中的位数（即进行逐位左移并舍弃移出目标类型的位）。
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx14"><td>
<p>对于有符号的非负 <code>a</code>，<code>a &lt;&lt; b</code> 的值是 <span class="texhtml" style="white-space: nowrap;">a * 2<span class="t-su">b<br></span></span>，若它能以返回类型表示，否则行为未定义。
</p>
</td>
<td><span class="t-mark-rev t-until-cxx14">(C++14 前)</span></td></tr>
<tr class="t-rev t-since-cxx14 t-until-cxx20"><td>
<p>对于有符号的非负 <code>a</code>，若 <span class="texhtml" style="white-space: nowrap;">a * 2<span class="t-su">b<br></span></span> 能以返回类型的无符号版本表示，则将该值<a href="implicit_cast.html#.E6.95.B4.E5.9E.8B.E8.BD.AC.E6.8D.A2" title="cpp/language/implicit conversion">转换</a>到有符号后即是 <code>a &lt;&lt; b</code> 的值（这使得以 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="nu0">1</span><span class="sy1">&lt;&lt;</span><span class="nu0">31</span></span></span> 创建 <span class="t-lc"><a href="../types/climits.html" title="cpp/types/climits">INT_MIN</a></span> 合法）；否则行为未定义。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx14">(C++14 起)</span><br><span class="t-mark-rev t-until-cxx20">(C++20 前)</span></td></tr>
</table>
<p>对于负 <code>a</code>，<code>a &lt;&lt; b</code> 的行为未定义。
</p><p>对于无符号 <code>a</code> 和有符号的非负 <code>a</code>，<code>a &gt;&gt; b</code> 的值是 <span class="texhtml" style="white-space: nowrap;">a/2<span class="t-su">b<br></span></span> 的整数部分。
</p><p>对于负 <code>a</code>，<code>a &gt;&gt; b</code> 的值是实现定义的（大多数平台上进行算术右移，故结果保留为负）。
</p>
</td>
<td><span class="t-mark-rev t-until-cxx20">(C++20 前)</span></td></tr>
<tr class="t-rev t-since-cxx20"><td>
<p><code>a &lt;&lt; b</code> 的值是与 <span class="texhtml" style="white-space: nowrap;">a * 2<span class="t-su">b<br></span></span> 对 <span class="texhtml" style="white-space: nowrap;">2<span class="t-su">N<br></span></span> 同余的唯一值，其中 <span class="texhtml" style="white-space: nowrap;">N</span> 是返回类型中的位数（即进行逐位左移并舍弃移出目标类型的位）。
</p><p><code>a &gt;&gt; b</code> 的值是 <span class="texhtml" style="white-space: nowrap;">a/2<span class="t-su">b<br></span></span> 向下取整（换言之，有符号 <code>a</code> 上的右移是算术右移）。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<p>任何情况下，若右操作数的值为负或大于等于提升后左操作数中的位数，则行为未定义。
</p><p>在<a href="overload_resolution.html#.E8.B0.83.E7.94.A8.E9.87.8D.E8.BD.BD.E8.BF.90.E7.AE.97.E7.AC.A6" title="cpp/language/overload resolution">针对用户定义运算符的重载决议</a>中，对于每对提升后的整型类型 <code>L</code> 和 <code>R</code>，下列函数签名参与重载决议：
</p>
<table class="t-dcl-begin"><tbody>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">L operator<span class="sy1">&lt;&lt;</span><span class="br0">(</span>L, R<span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr>
<tr class="t-dcl">
<td class="t-dcl-nopad"> <div><span class="mw-geshi cpp source-cpp">L operator<span class="sy1">&gt;&gt;</span><span class="br0">(</span>L, R<span class="br0">)</span></span></div></td>
<td class="t-dcl-nopad">  </td>
<td class="t-dcl-nopad">  </td>
</tr> 
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
</tbody></table>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
<span class="kw2">enum</span> <span class="br0">{</span>ONE<span class="sy1">=</span><span class="nu0">1</span>, TWO<span class="sy1">=</span><span class="nu0">2</span><span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <a href="../io/manip/hex.html"><span class="kw1782">std::<span class="me2">hex</span></span></a> <span class="sy1">&lt;&lt;</span> <a href="../io/manip/showbase.html"><span class="kw1766">std::<span class="me2">showbase</span></span></a><span class="sy4">;</span>
    <span class="kw4">char</span> c <span class="sy1">=</span> <span class="nu12">0x10</span><span class="sy4">;</span>
    <span class="kw4">unsigned</span> <span class="kw4">long</span> <span class="kw4">long</span> ull <span class="sy1">=</span> <span class="nu12">0x123</span><span class="sy4">;</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"0x123 &lt;&lt; 1 = "</span> <span class="sy1">&lt;&lt;</span> <span class="br0">(</span>ull <span class="sy1">&lt;&lt;</span> <span class="nu0">1</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span>
              <span class="sy1">&lt;&lt;</span> <span class="st0">"0x123 &lt;&lt; 63 = "</span> <span class="sy1">&lt;&lt;</span> <span class="br0">(</span>ull <span class="sy1">&lt;&lt;</span> <span class="nu0">63</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span> <span class="co1">// 无符号中的溢出</span>
              <span class="sy1">&lt;&lt;</span> <span class="st0">"0x10 &lt;&lt; 10 = "</span> <span class="sy1">&lt;&lt;</span> <span class="br0">(</span>c <span class="sy1">&lt;&lt;</span> <span class="nu0">10</span><span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>   <span class="co1">// 提升 char 到 int</span>
    <span class="kw4">long</span> <span class="kw4">long</span> ll <span class="sy1">=</span> <span class="sy2">-</span><span class="nu0">1000</span><span class="sy4">;</span>
    <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <a href="../io/manip/hex.html"><span class="kw1781">std::<span class="me2">dec</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"-1000 &gt;&gt; 1 = "</span> <span class="sy1">&lt;&lt;</span> <span class="br0">(</span>ll <span class="sy1">&gt;&gt;</span> ONE<span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">0x123 &lt;&lt; 1 = 0x246
0x123 &lt;&lt; 63 = 0x8000000000000000
0x10 &lt;&lt; 10 = 0x4000
-1000 &gt;&gt; 1 = -500</pre></div></div> 
</div>
<h3><span class="mw-headline" id=".E6.A0.87.E5.87.86.E5.BA.93">标准库</span></h3>
<p>许多标准库类型都重载了算术运算符。
</p>
<h4><span class="mw-headline" id=".E4.B8.80.E5.85.83.E7.AE.97.E6.9C.AF.E8.BF.90.E7.AE.97.E7.AC.A6_2">一元算术运算符</span></h4>
<table class="t-dsc-begin">

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../chrono/duration/operator_arith.html" title="cpp/chrono/duration/operator arith"> <span class="t-lines"><span>operator+</span><span>operator-</span></span></a></div></div>
</td>
<td>  实现一元 + 和一元 - <br> <span class="t-mark">(<code>std::chrono::duration&lt;Rep,Period&gt;</code> 的公开成员函数)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../numeric/complex/operator_arith2.html" title="cpp/numeric/complex/operator arith2"> <span class="t-lines"><span>operator+</span><span>operator-</span></span></a></div></div>
</td>
<td>  对复数运用一元运算符 <br> <span class="t-mark">(函数模板)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../numeric/valarray/operator_arith.html" title="cpp/numeric/valarray/operator arith"> <span class="t-lines"><span>operator+</span><span>operator-</span><span>operator~</span><span>operator!</span></span></a></div></div>
</td>
<td>   对 valarray 的每个元素运用一元算术运算符 <br> <span class="t-mark">(<code>std::valarray&lt;T&gt;</code> 的公开成员函数)</span> </td></tr>
</table>
<h4><span class="mw-headline" id=".E5.8A.A0.E6.80.A7.E8.BF.90.E7.AE.97.E7.AC.A6_2">加性运算符</span></h4>
<table class="t-dsc-begin">

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../chrono/time_point/operator_arith2.html" title="cpp/chrono/time point/operator arith2"> <span class="t-lines"><span>operator+</span><span>operator-</span></span></a></div></div>
</td>
<td>  实施涉及时间点的加法和减法运算 <br> <span class="t-mark">(函数模板)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../chrono/duration/operator_arith4.html" title="cpp/chrono/duration/operator arith4"> <span class="t-lines"><span>operator+</span><span>operator-</span><span>operator*</span><span>operator/</span><span>operator%</span></span></a></div></div>
</td>
<td>  实现以时长为实参的算术运算 <br> <span class="t-mark">(函数模板)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../chrono/year_month_day/operator_arith_2.html" title="cpp/chrono/year month day/operator arith 2"> <span class="t-lines"><span>operator+</span><span>operator-</span></span></a></div></div>
</td>
<td>  将 <code>year_month_day</code> 与一定量的年数或月数相加或相减 <br> <span class="t-mark">(<code>std::chrono::year_month_day</code> 的公开成员函数)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../string/basic_string/operator%2B.html" title="cpp/string/basic string/operator+"> <span class="t-lines"><span>operator+</span></span></a></div></div>
</td>
<td>   连接两个字符串或者一个字符串和一个字符  <br> <span class="t-mark">(函数模板)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../iterator/reverse_iterator/operator_arith.html" title="cpp/iterator/reverse iterator/operator arith"> <span class="t-lines"><span>operator+</span><span>operator-</span></span></a></div></div>
</td>
<td>  推进或减小迭代器 <br> <span class="t-mark">(<code>std::reverse_iterator&lt;Iter&gt;</code> 的公开成员函数)</span>
</td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../iterator/move_iterator/operator_arith.html" title="cpp/iterator/move iterator/operator arith"> <span class="t-lines"><span>operator+</span><span>operator-</span></span></a></div></div>
</td>
<td>  推进或减小迭代器 <br> <span class="t-mark">(<code>std::move_iterator&lt;Iter&gt;</code> 的公开成员函数)</span>
</td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../numeric/complex/operator_arith3.html" title="cpp/numeric/complex/operator arith3"> <span class="t-lines"><span>operator+</span><span>operator-</span><span>operator*</span><span>operator/</span></span></a></div></div>
</td>
<td>  在两个复数，或一个复数与一个标量上进行复数算术运算 <br> <span class="t-mark">(函数模板)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../numeric/valarray/operator_arith3.html" title="cpp/numeric/valarray/operator arith3"> <span class="t-lines"><span>operator+</span><span>operator-</span><span>operator*</span><span>operator/</span><span>operator%</span><span>operator&amp;</span><span>operator|</span><span>operator^</span><span>operator&lt;&lt;</span><span>operator&gt;&gt;</span><span>operator&amp;&amp;</span><span>operator||</span></span></a></div></div>
</td>
<td>   对两个 valarray 的每个元素，或一个 valarray 的每个元素和一个值运用二元运算符  <br> <span class="t-mark">(函数模板)</span> </td></tr>
</table>
<h4><span class="mw-headline" id=".E4.B9.98.E6.80.A7.E8.BF.90.E7.AE.97.E7.AC.A6_2">乘性运算符</span></h4>
<table class="t-dsc-begin">

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../chrono/duration/operator_arith4.html" title="cpp/chrono/duration/operator arith4"> <span class="t-lines"><span>operator+</span><span>operator-</span><span>operator*</span><span>operator/</span><span>operator%</span></span></a></div></div>
</td>
<td>  实现以时长为实参的算术运算 <br> <span class="t-mark">(函数模板)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../numeric/complex/operator_arith3.html" title="cpp/numeric/complex/operator arith3"> <span class="t-lines"><span>operator+</span><span>operator-</span><span>operator*</span><span>operator/</span></span></a></div></div>
</td>
<td>  在两个复数，或一个复数与一个标量上进行复数算术运算 <br> <span class="t-mark">(函数模板)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../numeric/valarray/operator_arith3.html" title="cpp/numeric/valarray/operator arith3"> <span class="t-lines"><span>operator+</span><span>operator-</span><span>operator*</span><span>operator/</span><span>operator%</span><span>operator&amp;</span><span>operator|</span><span>operator^</span><span>operator&lt;&lt;</span><span>operator&gt;&gt;</span><span>operator&amp;&amp;</span><span>operator||</span></span></a></div></div>
</td>
<td>   对两个 valarray 的每个元素，或一个 valarray 的每个元素和一个值运用二元运算符  <br> <span class="t-mark">(函数模板)</span> </td></tr>
</table>
<h4><span class="mw-headline" id=".E9.80.90.E4.BD.8D.E9.80.BB.E8.BE.91.E8.BF.90.E7.AE.97.E7.AC.A6_2">逐位逻辑运算符</span></h4>
<table class="t-dsc-begin">

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../utility/bitset/operator_logic.html" title="cpp/utility/bitset/operator logic"> <span class="t-lines"><span>operator&amp;=</span><span>operator|=</span><span>operator^=</span><span>operator~</span></span></a></div></div>
</td>
<td>  进行二进制与、或、异或及非 <br> <span class="t-mark">(<code>std::bitset&lt;N&gt;</code> 的公开成员函数)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../utility/bitset/operator_logic2.html" title="cpp/utility/bitset/operator logic2"> <span class="t-lines"><span>operator&amp;</span><span>operator|</span><span>operator^</span></span></a></div></div>
</td>
<td>   在 bitset 上执行二元逻辑操作 <br> <span class="t-mark">(函数)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../numeric/valarray/operator_arith.html" title="cpp/numeric/valarray/operator arith"> <span class="t-lines"><span>operator~</span></span></a></div></div>
</td>
<td>  对 valarray 的每个元素运用一元算术运算符 <br> <span class="t-mark">(<code>std::valarray&lt;T&gt;</code> 的公开成员函数)</span>
</td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../numeric/valarray/operator_arith3.html" title="cpp/numeric/valarray/operator arith3"> <span class="t-lines"><span>operator^</span><span>operator&amp;</span><span>operator|</span></span></a></div></div>
</td>
<td>  对两个 valarray 的每个元素，或一个 valarray 的每个元素和一个值运用二元运算符 <br> <span class="t-mark">(函数模板)</span>
</td></tr>
</table>
<h4><span class="mw-headline" id=".E7.A7.BB.E4.BD.8D.E8.BF.90.E7.AE.97.E7.AC.A6_2">移位运算符</span></h4>
<table class="t-dsc-begin">

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../numeric/valarray/operator_arith3.html" title="cpp/numeric/valarray/operator arith3"> <span class="t-lines"><span>operator&lt;&lt;</span><span>operator&gt;&gt;</span></span></a></div></div>
</td>
<td>  对两个 valarray 的每个元素，或一个 valarray 的每个元素和一个值运用二元运算符 <br> <span class="t-mark">(函数模板)</span>
</td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../utility/bitset/operator_ltltgtgt.html" title="cpp/utility/bitset/operator ltltgtgt"> <span class="t-lines"><span>operator&lt;&lt;</span><span>operator&gt;&gt;</span></span></a></div></div>
</td>
<td>  进行二进制左移和右移 <br> <span class="t-mark">(<code>std::bitset&lt;N&gt;</code> 的公开成员函数)</span>
</td></tr>
</table>
<h4><span class="mw-headline" id=".E6.B5.81.E6.8F.92.E5.85.A5.2F.E6.8A.BD.E5.8F.96.E8.BF.90.E7.AE.97.E7.AC.A6">流插入/抽取运算符</span></h4>
<p>整个标准库中，移位运算符常对 I/O 流（ <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../io/ios_base.html"><span class="kw1669">std::<span class="me2">ios_base</span></span></a><span class="sy3">&amp;</span></span></span> 或从它派生的类之一）重载，以之为左运算数和返回类型。这种运算符被称为<i>流插入</i>和<i>流抽取</i>运算符：
</p>
<table class="t-dsc-begin">

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../io/basic_istream/operator_gtgt.html" title="cpp/io/basic istream/operator gtgt"> <span class="t-lines"><span>operator&gt;&gt;</span></span></a></div></div>
</td>
<td>  提取带格式数据 <br> <span class="t-mark">(<code>std::basic_istream&lt;CharT,Traits&gt;</code> 的公开成员函数)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../io/basic_istream/operator_gtgt2.html" title="cpp/io/basic istream/operator gtgt2"> <span class="t-lines"><span>operator&gt;&gt;<span class="t-dsc-small">(std::basic_istream)</span></span></span></a></div></div>
</td>
<td>  提取字符和字符数组 <br> <span class="t-mark">(函数模板)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../io/basic_ostream/operator_ltlt.html" title="cpp/io/basic ostream/operator ltlt"> <span class="t-lines"><span>operator&lt;&lt;</span></span></a></div></div>
</td>
<td>  插入带格式数据 <br> <span class="t-mark">(<code>std::basic_ostream&lt;CharT,Traits&gt;</code> 的公开成员函数)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../io/basic_ostream/operator_ltlt2.html" title="cpp/io/basic ostream/operator ltlt2"> <span class="t-lines"><span>operator&lt;&lt;<span class="t-dsc-small">(std::basic_ostream)</span></span></span></a></div></div>
</td>
<td>  插入字符数据 <br> <span class="t-mark">(函数)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../numeric/complex/operator_ltltgtgt.html" title="cpp/numeric/complex/operator ltltgtgt"> <span class="t-lines"><span>operator&lt;&lt;</span><span>operator&gt;&gt;</span></span></a></div></div>
</td>
<td>  复数的序列化和反序列化 <br> <span class="t-mark">(函数模板)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../utility/bitset/operator_ltltgtgt2.html" title="cpp/utility/bitset/operator ltltgtgt2"> <span class="t-lines"><span>operator&lt;&lt;</span><span>operator&gt;&gt;</span></span></a></div></div>
</td>
<td>   执行 bitset 的流输入和输出 <br> <span class="t-mark">(函数)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../string/basic_string/operator_ltltgtgt.html" title="cpp/string/basic string/operator ltltgtgt"> <span class="t-lines"><span>operator&lt;&lt;</span><span>operator&gt;&gt;</span></span></a></div></div>
</td>
<td>  执行字符串的流输入与输出 <br> <span class="t-mark">(函数模板)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../numeric/random/linear_congruential_engine/operator_ltltgtgt.html" title="cpp/numeric/random/linear congruential engine/operator ltltgtgt"> <span class="t-lines"><span>operator&lt;&lt;</span><span>operator&gt;&gt;</span></span></a></div></div>
</td>
<td>  执行伪随机数引擎的流输入和输出 <br> <span class="t-mark">(函数)</span> </td></tr>

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../numeric/random/uniform_int_distribution/operator_ltltgtgt.html" title="cpp/numeric/random/uniform int distribution/operator ltltgtgt"> <span class="t-lines"><span>operator&lt;&lt;</span><span>operator&gt;&gt;</span></span></a></div></div>
</td>
<td>  执行伪随机数分布的流输入和输出 <br> <span class="t-mark">(函数模板)</span> </td></tr>
</table>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<p><a href="operator_precedence.html" title="cpp/language/operator precedence">运算符优先级</a>
</p><p><a href="operators.html" title="cpp/language/operators">运算符重载</a>
</p>
<table class="wikitable">

<tr style="text-align:center">
<th colspan="7">常见运算符
</th></tr>
<tr style="text-align:center">
<td> <a href="operator_assignment.html" title="cpp/language/operator assignment">赋值</a>
</td>
<td> <a href="operator_incdec.html" title="cpp/language/operator incdec">自增<br>自减</a>
</td>
<td> <strong class="selflink">算术</strong>
</td>
<td> <a href="operator_logical.html" title="cpp/language/operator logical">逻辑</a>
</td>
<td> <a href="operator_comparison.html" title="cpp/language/operator comparison">比较</a>
</td>
<td> <a href="operator_member_access.html" title="cpp/language/operator member access">成员访问</a>
</td>
<td> <a href="operator_other.html" title="cpp/language/operator other">其他</a>
</td></tr>
<tr style="text-align:center">
<td>
<p><span class="t-c"><span class="mw-geshi cpp source-cpp">a <span class="sy1">=</span> b<br>
a <span class="sy2">+</span><span class="sy1">=</span> b<br>
a <span class="sy2">-</span><span class="sy1">=</span> b<br>
a <span class="sy2">*</span><span class="sy1">=</span> b<br>
a <span class="sy2">/</span><span class="sy1">=</span> b<br>
a <span class="sy2">%</span><span class="sy1">=</span> b<br>
a <span class="sy3">&amp;</span><span class="sy1">=</span> b<br>
a <span class="sy3">|</span><span class="sy1">=</span> b<br>
a <span class="sy3">^</span><span class="sy1">=</span> b<br>
a <span class="sy1">&lt;&lt;=</span> b<br>
a <span class="sy1">&gt;&gt;=</span> b</span></span>
</p>
</td>
<td>
<p><span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy2">++</span>a<br>
<span class="sy2">--</span>a<br>
a<span class="sy2">++</span><br>
a<span class="sy2">--</span></span></span>
</p>
</td>
<td>
<p><span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy2">+</span>a<br>
<span class="sy2">-</span>a<br>
a <span class="sy2">+</span> b<br>
a <span class="sy2">-</span> b<br>
a <span class="sy2">*</span> b<br>
a <span class="sy2">/</span> b<br>
a <span class="sy2">%</span> b<br>
~a<br>
a <span class="sy3">&amp;</span> b<br>
a <span class="sy3">|</span> b<br>
a <span class="sy3">^</span> b<br>
a <span class="sy1">&lt;&lt;</span> b<br>
a <span class="sy1">&gt;&gt;</span> b</span></span>
</p>
</td>
<td>
<p><span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy3">!</span>a<br>
a <span class="sy3">&amp;&amp;</span> b<br>
a <span class="sy3">||</span> b</span></span>
</p>
</td>
<td>
<p><span class="t-c"><span class="mw-geshi cpp source-cpp">a <span class="sy1">==</span> b<br>
a <span class="sy3">!</span><span class="sy1">=</span> b<br>
a <span class="sy1">&lt;</span> b<br>
a <span class="sy1">&gt;</span> b<br>
a <span class="sy1">&lt;=</span> b<br>
a <span class="sy1">&gt;=</span> b<br>
a <span class="sy1">&lt;=&gt;</span> b</span></span>
</p>
</td>
<td>
<p><span class="t-c"><span class="mw-geshi cpp source-cpp">a<span class="br0">[</span>b<span class="br0">]</span><br>
<span class="sy2">*</span>a<br>
<span class="sy3">&amp;</span>a<br>
a<span class="sy2">-</span><span class="sy1">&gt;</span>b<br>
a.<span class="me1">b</span><br>
a<span class="sy2">-</span><span class="sy1">&gt;</span><span class="sy2">*</span>b<br>
a.<span class="sy2">*</span>b</span></span>
</p>
</td>
<td>
<p><span class="t-c"><span class="mw-geshi cpp source-cpp">a<span class="br0">(</span>...<span class="br0">)</span><br>
a, b<br>
<span class="sy4">?</span> <span class="sy4">:</span></span></span>
</p>
</td></tr>
<tr>
<th colspan="7">特殊运算符
</th></tr>
<tr>
<td colspan="7">
<p><a href="static_cast.html" title="cpp/language/static cast"><tt>static_cast</tt></a> 转换一个类型为另一相关类型<br>
<a href="dynamic_cast.html" title="cpp/language/dynamic cast"><tt>dynamic_cast</tt></a> 在继承层级中转换<br>
<a href="const_cast.html" title="cpp/language/const cast"><tt>const_cast</tt></a> 添加或移除 <a href="cv.html" title="cpp/language/cv">cv</a> 限定符<br>
<a href="reinterpret_cast.html" title="cpp/language/reinterpret cast"><tt>reinterpret_cast</tt></a> 转换类型到无关类型<br>
<a href="explicit_cast.html" title="cpp/language/explicit cast">C 风格转型</a> 以 <code>static_cast</code> 、 <code>const_cast</code> 及 <code>reinterpret_cast</code> 的混合转换一个类型到另一类型<br>
<a href="new.html" title="cpp/language/new"><tt>new</tt></a> 创建有动态存储期的对象<br>
<a href="delete.html" title="cpp/language/delete"><tt>delete</tt></a> 销毁先前由 new 表达式创建的对象，并释放其所拥有的内存区域<br>
<a href="sizeof.html" title="cpp/language/sizeof"><tt>sizeof</tt></a> 查询类型的大小<br>
<a href="sizeof....html" title="cpp/language/sizeof..."><tt>sizeof...</tt></a> 查询<a href="parameter_pack.html" title="cpp/language/parameter pack">形参包</a>的大小<span class="t-mark-rev t-since-cxx11">(C++11 起)</span><br>
<a href="typeid.html" title="cpp/language/typeid"><tt>typeid</tt></a> 查询类型的类型信息<br>
<a href="noexcept.html" title="cpp/language/noexcept"><tt>noexcept</tt></a> 查询表达式是否能抛出异常<span class="t-mark-rev t-since-cxx11">(C++11 起)</span><br>
<a href="alignof.html" title="cpp/language/alignof"><tt>alignof</tt></a> 查询类型的对齐要求<span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</p>
</td></tr></table>
<table class="t-dsc-begin">

</table>

<!-- 
NewPP limit report
Preprocessor visited node count: 12407/1000000
Preprocessor generated node count: 18820/1000000
Post‐expand include size: 245195/2097152 bytes
Template argument size: 65722/2097152 bytes
Highest expansion depth: 32/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:2735-0!*!0!!zh!*!zh!* and timestamp 20200208232209 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/operator_arithmetic&amp;oldid=62662">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/operator_arithmetic&amp;oldid=62662</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/operator_arithmetic">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2019年9月6日 (星期五) 18:00。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过9,580次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 6.333 secs. -->
	</body>
<!-- Cached 20200208232209 -->
</html>